﻿#ifndef __FOUNDATION_PREDEFINE_H__
#define __FOUNDATION_PREDEFINE_H__

// common macros
#ifndef NULL
#define NULL 0
#endif

#define IN
#define OUT

#define NEZHA_SIGNATURE "Nezha"
#define NEZHA_VERSION_MAJOR 0
#define NEZHA_VERSION_MINOR 1
#define NEZHA_VERSION_PATCH 0
#define NEZHA_VERSION_SUFFIX "Toread"
#define NEZHA_VERSION ((NEZHA_VERSION_MAJOR << 16) | (NEZHA_VERSION_MINOR << 8) | NEZHA_VERSION_PATCH)
#define NEZHA_SIGNATURE_STRING(s) (sprintf(s, "%s v%d.%d.%d %s", NEZHA_SIGNATURE, NEZHA_VERSION_MAJOR,\
	NEZHA_VERSION_MINOR, NEZHA_VERSION_PATCH, NEZHA_VERSION_SUFFIX))

#ifndef NO_CRT_SUPPORT

//#if defined(NZOS_WIN32)
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
//#endif

#endif

// STL
#include <vector>
#include <list>
#include <map>
#include <string>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <sstream>


// c lib
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>

using namespace std;

typedef unsigned char u8;
typedef short s16;
typedef unsigned short u16;
typedef unsigned int u32;
typedef float f32;
typedef double f64;


#ifdef _NEZHA_64
typedef unsigned long long uLong;
typedef f64 Real;
#else
typedef unsigned int uLong;
typedef f32 Real;
#endif

typedef long long s64;
typedef std::string _string;
typedef std::stringstream _stringStream;

// add by gleen 20121023 Ã·π©ª˘±æ»›∆˜÷ß≥÷
/**
typedef std::map		PMap;
typedef std::set		PSet;
typedef std::pair		MapPair;	
typedef std::list		PList;
typedef std::vector		PArray;	
typedef std::queue		PQueue;		
typedef std::deque		PDeque;

typedef std::less		PLessF;
typedef std::equal_to	PEqualF;	

typedef	std::vector		SimpleArray;
**/

#define PLessF		std::less
#define PEqualF		std::equal_to

#define PMap		std::map
#define PSet		std::set
#define PBitSet		std::bitset
#define MapPair		std::pair
#define PList		std::list
#define PArray		std::vector
#define PQueue		std::queue
#define PDeque		std::deque


struct ci_char_traits : public std::char_traits<char>
{
	static bool equal( char c1, char c2 )
	{ 
		return toupper(c1) == toupper(c2); 
	}

	static bool notEqual( char c1, char c2 )
	{ 
		return toupper(c1) != toupper(c2); 
	}

	static bool less( char c1, char c2 )
	{ 
		return toupper(c1) <  toupper(c2); 
	}

	static int compare( const char* s1, const char* s2, size_t n ) 
	{
#if (NZOS_IOS)
        return memcmp( s1, s2, n );
#else
		return _memicmp( s1, s2, n );
#endif
	}

	static const char* find( const char* s, int n, char a )
	{
		int al = toupper(a);
		while( n-- > 0 && toupper(*s) != al ) 
		{
			++s;
		}
		return equal(*s, a) ? s : 0;
	}
};

#define NEZHA_DELETE(p) if(p){NZ_Delete p;p = NULL;}
#define NEZHA_DELETE_ARRAY(p) if(p){NZ_Delete []p;p = NULL;}

namespace Nezha
{

	struct _NzStrLess
	{
		bool operator() (const _string& a, const _string& b) const
		{
			const size_t la = a.length();
			const size_t lb = b.length();

			if (la == lb)
				return (memcmp(a.c_str(), b.c_str(), la*sizeof(char)) < 0);

			return (la < lb);
		}
	};

	typedef std::basic_string<char, ci_char_traits>					IString;
	typedef _string															String;

	// ºÚ“◊∑‚◊∞“ªœ¬
	typedef PArray<String>													StringArray;
	typedef PArray<IString>												IStringArray;

	typedef PMap<_string, _string, _NzStrLess>						Str_KeyVals;
	typedef MapPair<_string, _string>									Str_KVPair;
	typedef PArray<Str_KVPair>											Str_KeyValArray;

	inline _string Str_KeyVals_Val(const Str_KeyVals& kvs, const _string& key)
	{
		Str_KeyVals::const_iterator it = kvs.find(key);
		if(it != kvs.end())
		{
			return it->second;
		}

		return "";
	}
};

#define NEZHA_INTERNAL public

//macro to controll if use replacement fast evaluation funcs
// 1 use fast but low precision version, 2 use fastest version
//#define USE_FAST_EVALUATION_FUNC 2

#endif //end __FOUNDATION_PREDEFINE_H__